package com.vision.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vision.mapper.CommentMapper;
import com.vision.pojo.vo.CommentVO;
import com.vision.pojo.vo.PageVO;
import com.vision.service.CommentService;
import com.vision.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @projectName: vision
 * @package: com.vision.service.impl
 * @className: CommentServiceImpl
 * @author: escapeicon@gmail.com
 * @description: 评论服务实现
 * @date: 2024/1/25 20:27
 * @version: 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 添加评论
     * @param videoId 视频id
     * @param content 评论内容
     * @param parentCommentId 父评论id
     * @param replyUserId 回复用户id
     */
    @Override
    public CommentVO addComment(Integer videoId, String content, Integer parentCommentId, Integer replyUserId) {
        // 1. 获取当前登录用户 ID
        Map<String, Integer> claims = ThreadLocalUtil.get();
        Integer userId= claims.get("id");
        // 2. 添加评论
        commentMapper.insert(userId,videoId,content,parentCommentId,replyUserId);
        // 3. 查询评论
        return commentMapper.selectCommentSingle(userId,videoId,parentCommentId,replyUserId);
    }

    /**
     * 获取父评论
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param videoId 视频id
     * @return com.vision.pojo.vo.PageVO<com.vision.pojo.vo.CommentVO>
     */
    @Override
    public PageVO<CommentVO> getParentComment(Integer pageNum, Integer pageSize, Integer videoId, Integer userId) {
        // 1. 创建 pageVO 对象
        PageVO<CommentVO> pageVO = new PageVO<>();
        // 2. 开启分页
        PageHelper.startPage(pageNum,pageSize);
        // 3. 查询父评论
        List<CommentVO> list = commentMapper.selectParentComment(videoId,userId);
        // 4. 向下转型
        Page<CommentVO> commentList = (Page<CommentVO>) list;
        // 5. 设置数据
        pageVO.setItems(commentList.getResult());
        pageVO.setTotal(commentList.getTotal());
        pageVO.setPageNum(pageNum);
        pageVO.setPageSize(pageSize);
        // 6. 返回结果
        return pageVO;
    }

    /**
     * 获取子评论
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param videoId 视频id
     * @param parentId 父评论id
     * @param userId 当前登录用户id
     * @return com.vision.pojo.vo.PageVO<com.vision.pojo.vo.CommentVO>
     */
    @Override
    public PageVO<CommentVO> getSonComment(Integer pageNum, Integer pageSize, Integer videoId, Integer parentId, Integer userId) {
        // 1. 创建 pageVO 对象
        PageVO<CommentVO> pageVO = new PageVO<>();
        // 2. 开启分页
        PageHelper.startPage(pageNum,pageSize);
        // 3. 查询子评论
        List<CommentVO> list = commentMapper.selectSonComment(videoId,parentId,userId);
        // 4. 向下转型
        Page<CommentVO> commentList = (Page<CommentVO>) list;
        // 5. 设置数据
        pageVO.setItems(commentList.getResult());
        pageVO.setTotal(commentList.getTotal());
        pageVO.setPageNum(pageNum);
        pageVO.setPageSize(pageSize);
        // 6. 返回结果
        return pageVO;
    }
}
